package com.xmmj.baselibrary.utils

import android.text.TextUtils
import android.util.Log

/**
 * 自定义日志输出工具类
 */
object Logger {
    private const val TAG = "Logger"
    private const val SHOW_LOG = 0
    private const val HIDE_LOG = 7
    private var LOG_LEVEL = SHOW_LOG
    private const val VERBOSE = Log.VERBOSE
    private const val DEBUG = Log.DEBUG
    private const val INFO = Log.INFO
    private const val WARN = Log.WARN
    private const val ERROR = Log.ERROR

    @JvmStatic
    fun setDevelopMode(debug: Boolean) {
        LOG_LEVEL = if (debug) {
            SHOW_LOG
        } else {
            HIDE_LOG
        }
    }

    fun setLogLevel(logLevel: Int) {
        LOG_LEVEL = logLevel
    }

    private fun v(tag: String?, msg: String?) {
        if (LOG_LEVEL <= VERBOSE && !TextUtils.isEmpty(msg)) {
            Log.v(tag, msg!!)
        }
    }

    private fun d(tag: String?, msg: String?) {
        if (LOG_LEVEL <= DEBUG && !TextUtils.isEmpty(msg)) {
            Log.d(tag, msg!!)
        }
    }

    private fun i(tag: String?, msg: String?) {
        if (LOG_LEVEL <= INFO && !TextUtils.isEmpty(msg)) {
            Log.i(tag, msg!!)
        }
    }

    private fun w(tag: String?, msg: String?) {
        if (LOG_LEVEL <= WARN && !TextUtils.isEmpty(msg)) {
            Log.w(tag, msg!!)
        }
    }

    private fun e(tag: String?, msg: String?) {
        if (LOG_LEVEL <= ERROR && !TextUtils.isEmpty(msg)) {
            Log.e(tag, msg!!)
        }
    }

    private fun e(tag: String?, throwable: Throwable?) {
        if (LOG_LEVEL <= ERROR && throwable != null) {
            Log.e(tag, throwable.localizedMessage)
        }
    }

    fun v(str: String?) {
        v(TAG, str)
    }

    fun d(str: String?) {
        d(TAG, str)
    }

    fun i(str: String?) {
        i(TAG, str)
    }

    fun w(str: String?) {
        w(TAG, str)
    }

    fun e(str: String?) {
        e(TAG, str)
    }
}